{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bf04f16d",
   "metadata": {},
   "source": [
    "* 1. 尝试创建detect.py 并实现功能调用\n",
    "* 2. 尝试创建analyze.py/campare.py/search.py,并实现功能调用\n",
    "* 3. 尝试存放宿舍四位同学的人脸信息到 人脸数据库（xxx栋xxx宿舍）：\n",
    "> 1. 完善faceset信息\n",
    "> 2. 实现人脸搜索和人脸对比，打印结果：是本人OR不是本人\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b2006e4",
   "metadata": {},
   "source": [
    "### 1.尝试创建detect.py 并实现功能调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "403e0e4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = \"N4jJQNtBkzNgVcOqZV-P34mKhYPm0hk2\"\n",
    "API_secret = \"nNhPtXGnphFeKs0o7E_tALIoyuQ2hh7e\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ae373b7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "03e81556",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648390117,34d44ef2-e725-47b0-b393-f0a394f54e1e',\n",
       " 'time_used': 78,\n",
       " 'faces': [{'face_token': 'b0111b25d1e34e9903da96dd372a5d28',\n",
       "   'face_rectangle': {'top': 98, 'left': 164, 'width': 118, 'height': 118}}],\n",
       " 'image_id': 'fwD9nHfTb9s3JIGwQOQmSA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_key,API_secret,\"Liu_01.jpeg\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8abea44",
   "metadata": {},
   "source": [
    "### 2.尝试创建analyze.py/campare.py/search.py,并实现功能调用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46d27e97",
   "metadata": {},
   "source": [
    "#### analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2757710b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9e29845c",
   "metadata": {},
   "outputs": [],
   "source": [
    "face1 = detect.face_detect(API_key,API_secret,\"anni-1.jpg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d36ab149",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'20265ed0a50f4651d699589823a948a1'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_tokens1 = face1[\"faces\"][0][\"face_token\"]\n",
    "face_tokens1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7036ec05",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module analyze:\n",
      "\n",
      "NAME\n",
      "    analyze\n",
      "\n",
      "FUNCTIONS\n",
      "    face_analyze(API_key, API_secret, face_tokens, attributes=None, landmark=1)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\lenovo\\jupyter project\\analyze.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(analyze)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1c0ad433",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 74,\n",
       " 'request_id': '1648390121,d9d36661-cce1-4a35-a5a1-3b3a5fd234cc',\n",
       " 'faces': [{'landmark': {'mouth_upper_lip_left_contour2': {'y': 453, 'x': 432},\n",
       "    'mouth_upper_lip_top': {'y': 443, 'x': 462},\n",
       "    'mouth_upper_lip_left_contour1': {'y': 442, 'x': 450},\n",
       "    'left_eye_upper_left_quarter': {'y': 331, 'x': 371},\n",
       "    'left_eyebrow_lower_middle': {'y': 303, 'x': 372},\n",
       "    'mouth_upper_lip_left_contour3': {'y': 461, 'x': 442},\n",
       "    'right_eye_top': {'y': 307, 'x': 511},\n",
       "    'left_eye_bottom': {'y': 347, 'x': 387},\n",
       "    'right_eyebrow_lower_left_quarter': {'y': 293, 'x': 491},\n",
       "    'right_eye_pupil': {'y': 316, 'x': 514},\n",
       "    'mouth_lower_lip_right_contour1': {'y': 462, 'x': 492},\n",
       "    'mouth_lower_lip_right_contour3': {'y': 483, 'x': 491},\n",
       "    'mouth_lower_lip_right_contour2': {'y': 472, 'x': 507},\n",
       "    'contour_chin': {'y': 538, 'x': 475},\n",
       "    'contour_left9': {'y': 534, 'x': 443},\n",
       "    'left_eye_lower_right_quarter': {'y': 344, 'x': 403},\n",
       "    'mouth_lower_lip_top': {'y': 466, 'x': 467},\n",
       "    'right_eyebrow_upper_middle': {'y': 272, 'x': 509},\n",
       "    'left_eyebrow_left_corner': {'y': 309, 'x': 339},\n",
       "    'right_eye_bottom': {'y': 330, 'x': 515},\n",
       "    'contour_left7': {'y': 504, 'x': 395},\n",
       "    'contour_left6': {'y': 483, 'x': 377},\n",
       "    'contour_left5': {'y': 458, 'x': 365},\n",
       "    'contour_left4': {'y': 431, 'x': 356},\n",
       "    'contour_left3': {'y': 404, 'x': 347},\n",
       "    'contour_left2': {'y': 377, 'x': 340},\n",
       "    'contour_left1': {'y': 349, 'x': 336},\n",
       "    'left_eye_lower_left_quarter': {'y': 346, 'x': 373},\n",
       "    'contour_right1': {'y': 313, 'x': 597},\n",
       "    'contour_right3': {'y': 374, 'x': 602},\n",
       "    'contour_right2': {'y': 343, 'x': 601},\n",
       "    'mouth_left_corner': {'y': 468, 'x': 420},\n",
       "    'contour_right4': {'y': 406, 'x': 601},\n",
       "    'contour_right7': {'y': 492, 'x': 567},\n",
       "    'right_eyebrow_left_corner': {'y': 301, 'x': 467},\n",
       "    'nose_right': {'y': 404, 'x': 490},\n",
       "    'nose_tip': {'y': 401, 'x': 453},\n",
       "    'contour_right5': {'y': 437, 'x': 597},\n",
       "    'nose_contour_lower_middle': {'y': 422, 'x': 460},\n",
       "    'left_eyebrow_lower_left_quarter': {'y': 303, 'x': 354},\n",
       "    'mouth_lower_lip_left_contour3': {'y': 487, 'x': 449},\n",
       "    'right_eye_right_corner': {'y': 318, 'x': 541},\n",
       "    'right_eye_lower_right_quarter': {'y': 326, 'x': 530},\n",
       "    'mouth_upper_lip_right_contour2': {'y': 446, 'x': 497},\n",
       "    'right_eyebrow_lower_right_quarter': {'y': 278, 'x': 534},\n",
       "    'left_eye_left_corner': {'y': 341, 'x': 362},\n",
       "    'mouth_right_corner': {'y': 458, 'x': 517},\n",
       "    'mouth_upper_lip_right_contour3': {'y': 456, 'x': 490},\n",
       "    'right_eye_lower_left_quarter': {'y': 330, 'x': 498},\n",
       "    'left_eyebrow_right_corner': {'y': 308, 'x': 414},\n",
       "    'left_eyebrow_lower_right_quarter': {'y': 307, 'x': 392},\n",
       "    'right_eye_center': {'y': 321, 'x': 513},\n",
       "    'nose_left': {'y': 414, 'x': 426},\n",
       "    'mouth_lower_lip_left_contour1': {'y': 467, 'x': 443},\n",
       "    'left_eye_upper_right_quarter': {'y': 329, 'x': 403},\n",
       "    'right_eyebrow_lower_middle': {'y': 283, 'x': 512},\n",
       "    'left_eye_top': {'y': 326, 'x': 386},\n",
       "    'left_eye_center': {'y': 339, 'x': 388},\n",
       "    'contour_left8': {'y': 521, 'x': 417},\n",
       "    'contour_right9': {'y': 530, 'x': 511},\n",
       "    'right_eye_left_corner': {'y': 329, 'x': 483},\n",
       "    'mouth_lower_lip_bottom': {'y': 488, 'x': 470},\n",
       "    'left_eyebrow_upper_left_quarter': {'y': 294, 'x': 351},\n",
       "    'left_eye_pupil': {'y': 335, 'x': 391},\n",
       "    'right_eyebrow_upper_left_quarter': {'y': 283, 'x': 486},\n",
       "    'contour_right8': {'y': 513, 'x': 541},\n",
       "    'right_eyebrow_right_corner': {'y': 278, 'x': 557},\n",
       "    'right_eye_upper_left_quarter': {'y': 314, 'x': 494},\n",
       "    'left_eyebrow_upper_middle': {'y': 293, 'x': 372},\n",
       "    'right_eyebrow_upper_right_quarter': {'y': 267, 'x': 534},\n",
       "    'nose_contour_left1': {'y': 336, 'x': 429},\n",
       "    'nose_contour_left2': {'y': 390, 'x': 429},\n",
       "    'mouth_upper_lip_right_contour1': {'y': 439, 'x': 474},\n",
       "    'nose_contour_right1': {'y': 331, 'x': 464},\n",
       "    'nose_contour_right2': {'y': 382, 'x': 479},\n",
       "    'mouth_lower_lip_left_contour2': {'y': 479, 'x': 433},\n",
       "    'contour_right6': {'y': 466, 'x': 586},\n",
       "    'nose_contour_right3': {'y': 414, 'x': 476},\n",
       "    'nose_contour_left3': {'y': 420, 'x': 442},\n",
       "    'left_eye_right_corner': {'y': 339, 'x': 417},\n",
       "    'left_eyebrow_upper_right_quarter': {'y': 296, 'x': 394},\n",
       "    'right_eye_upper_right_quarter': {'y': 309, 'x': 528},\n",
       "    'mouth_upper_lip_bottom': {'y': 458, 'x': 465}},\n",
       "   'face_rectangle': {'width': 263, 'top': 274, 'left': 346, 'height': 263},\n",
       "   'face_token': '20265ed0a50f4651d699589823a948a1'}]}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "analyze.face_analyze(API_key,API_secret,face_tokens1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1630743e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "73aa1b0d",
   "metadata": {},
   "source": [
    "#### campare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7a763c8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0d86c28b",
   "metadata": {},
   "outputs": [],
   "source": [
    "face2 = detect.face_detect(API_key,API_secret,\"anni-2.jpg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "913518f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'df1ebd65884587b4db28a02a16ad81f2'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_tokens2 = face2[\"faces\"][0][\"face_token\"]\n",
    "face_tokens2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "af3a4666",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module compare:\n",
      "\n",
      "NAME\n",
      "    compare\n",
      "\n",
      "FUNCTIONS\n",
      "    face_compare(API_key, API_secret, face_token1, face_token2)\n",
      "\n",
      "FILE\n",
      "    c:\\users\\lenovo\\jupyter project\\compare.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(compare)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e1c7b698",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648390130,5051a50b-9e7f-4f4d-889e-67dfbf571cef',\n",
       " 'time_used': 349,\n",
       " 'confidence': 90.057,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare.face_compare(API_key,API_secret,face_tokens1,face_tokens2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f66f6990",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "ea8d58a4",
   "metadata": {},
   "source": [
    "#### search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4849ce58",
   "metadata": {},
   "outputs": [],
   "source": [
    "import search"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18589537",
   "metadata": {},
   "source": [
    "* 创建人脸信息库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "18ce21b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8e8c24f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "payload = {\n",
    "    \"api_key\":API_key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"face_tokens\":\"c12973f90adb0e56b052bd5d79fdf324,b63dee7cf7909404419c08afe8c4e46c\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3e29f962",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(base_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "9f5b9bfc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'ee1d47f901cb51d5ecae3030c92c84fb',\n",
       " 'time_used': 676,\n",
       " 'face_count': 2,\n",
       " 'face_added': 2,\n",
       " 'request_id': '1648390133,7364acaf-21ae-47d9-82e9-f29ab31f3f03',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c0d754e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ee1d47f901cb51d5ecae3030c92c84fb'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceset_tokens = r.json()[\"faceset_token\"]\n",
    "faceset_tokens"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ad19c27",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "ed1b3fe7",
   "metadata": {},
   "source": [
    "* 搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "9b6e29dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648390135,c6cc432a-d81a-4e11-a226-1c8d65dc2688',\n",
       " 'time_used': 379,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'results': [{'confidence': 97.262,\n",
       "   'user_id': '',\n",
       "   'face_token': 'c12973f90adb0e56b052bd5d79fdf324'}]}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search.face_search(API_key,API_secret,faceset_tokens,face_tokens1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "513c5df6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
